<!--电子围栏页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;"
               @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>
  <div id="mapContainer" style="width:100%;height:84vh;"></div>
  <el-dialog title="围栏信息" v-model="dialogVisible" :before-close="closeDialog">
    <el-form label-width="80px">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入围栏名称" v-model="geofence.name"></el-input>
      </el-form-item>
    </el-form>
    <template>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="saveGeofence">确定</el-button>
    </template>

  </el-dialog>
</template>
<script setup>
import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
import {map} from "core-js/internals/array-iteration";

const dialogVisible = ref(false);
const geofence = ref({
  name:'',
  position:{}
})
onMounted(()=>{
  nextTick(()=>{
    var map = new BMapGL.Map("mapContainer")
    var point = new BMapGL.Point(116.4074,39.9024);
    map.centerAndZoom(point,15);

    const scene = new DrawScene(map);
    const drawContrl = new DrawControl(scene,{
      enableTips:false,
      anchor:BMAP_ANCHOR_TOP_RIGHT,
      drawingItems:[
          'circle',
          'rectangle'
      ]
    });
    scene.addEventListener(OperateEventType.COMPLETE,(event)=>{
      dialogVisible.value = true;
      console.log(event.target);
      let overlay = event.target.overlay;
      if(overlay instanceof BMapGL.Circle){
        geofence.value.position.type='circle';
        geofence.value.position.radius = overlay.getRadius();
        geofence.value.position.longitude = overlay.getCenter().lng;
        geofence.value.position.latitude = overlay.getCenter().lat;
        console.log(geofence.value);
        console.log(geofence.value.position);
      }else{
        let recPoints =[];
        for(var i = 0;i<4;i++){
          recPoints.push(overlay.points[i].latLng.lng+'-'+overlay.points[i].latLng.lat);
        }
        geofence.value.position.type = 'rectangle';
        geofence.value.position.recPoints.join(',');
        console.log(geofence.value);
        console.log(geofence.value.position);
      }
    })
    map.addControl(drawContrl);
  })
})

const saveGeofence=()=>{
  if(!geofence.value.name || geofence.value.name.trim.length==0){
    ElMessage.error('围栏名称不能为空')
    return;
  }
  geofence.value.position = JSON.stringify(geofence.value.position);
  let data = qs.stringify(geofence.value);
  axios.post(BASE_URL+'/v1/geofence/save',data).then((response)=>{
    if(response.data.code ===2000 ){
      ElMessage.success('保存成功！')
      dialogVisible.value = false;
      router.push('/geofence');
    }else{
      ElMessage.error(response.data.msg);
    }
  })
  console.log(geofence.value);
  console.log(geofence.value.name);
};
const closeDialog =()=>{
  if(confirm('确认关闭围栏吗？')){
    dialogVisible.value = false;
    map.clearOverlays();
    geofence.value={name:'',position:{}};
  }
};
</script>

<style scoped>
</style>